from bs4 import BeautifulSoup
from pptx import Presentation
from pptx.util import Inches

# 读取 HTML 文件
with open('ultrasonic_machining_ppt.html', 'r', encoding='utf-8') as file:
    html_content = file.read()

# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(html_content, 'html.parser')

# 创建一个新的 PPT 演示文稿
prs = Presentation()

# 幻灯片布局：标题幻灯片
title_slide_layout = prs.slide_layouts[0]
# 幻灯片布局：标题和内容幻灯片
content_slide_layout = prs.slide_layouts[1]

# 找到所有幻灯片
slides = soup.find_all('div', class_='slide')

for slide in slides:
    if 'title-slide' in slide.get('class', []):
        # 处理标题幻灯片
        slide_ppt = prs.slides.add_slide(title_slide_layout)
        title = slide.find('h1')
        subtitle = slide.find('p')
        if title:
            slide_ppt.shapes.title.text = title.text
        if subtitle:
            slide_ppt.placeholders[1].text = subtitle.text
    else:
        # 处理普通幻灯片
        slide_ppt = prs.slides.add_slide(content_slide_layout)
        title = slide.find(['h1', 'h2'])
        if title:
            slide_ppt.shapes.title.text = title.text

        content = ""
        paragraphs = slide.find_all(['p', 'li'])
        for p in paragraphs:
            content += p.text + "\n"

        if content:
            body_shape = slide_ppt.shapes.placeholders[1]
            tf = body_shape.text_frame
            tf.text = content

# 保存 PPT
prs.save('ultrasonic_machining_ppt.pptx')